#include<vector>
using namespace std;
class Solution
{
public:
    int longestOnes(vector<int> &nums, int k)
    {
        int res = 0;
        int l = 0, r = 0;
        while (r < nums.size())
        {
            if (nums[r])
            {
                r++;
            }
            else
            {
                if (k)
                {
                    r++;
                    k--;
                }
                else
                {
                    while (nums[l++])
                        ;
                    k++;
                }
            }
            res = max(res, r - l);
        }
        return res;
    }
};